<template>
    <div class="sales-view">
        <el-card shadow="hover" :body-style="{ padding:'0 0 20px 0' }">
            <template v-slot:header>
                <div class="menu-wrapper">
                    <!--
                        两个el-menu-item设置的index是字符串类型，如果下面的 activeIndex 直接填写数字为：
                        :default-active="1" 会报错，这里程序认作数字1了，所以如果使用立即数应该写为：
                        :default-active="'1'"
                     -->
                    <el-menu mode="horizontal" :default-active="activeIndex" @select="onMenuSelect"
                        class="sales-view-menu">
                        <el-menu-item index="1">销售额</el-menu-item>
                        <el-menu-item index="2">访问量</el-menu-item>
                    </el-menu>

                    <div class="menu-right">
                        <el-radio-group v-model="radioSelect" size="small">
                            <el-radio-button label="今日" />
                            <el-radio-button label="本周" />
                            <el-radio-button label="本月" />
                            <el-radio-button label="今年" />
                        </el-radio-group>

                        <!-- unlink-panels 取消前后两个日期选择框的关联关系 -->
                        <el-date-picker type="daterange" size="small" v-model="date" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"
                            unlink-panels class="sales-view-date-picker"/>
                    </div>
                </div>
            </template>

            <template><!-- 横向第二个card的图表 -->
                <div class="sales-view-chart-wrapper">
                    <v-chart :options="chartOption"/>

                    <div class="sales-view-list">
                        <div class="sales-view-title">排行榜</div>
                        <div class="list-item-wrapper">
                            <div class="list-item" v-for="item in rankData" :key="item.no">
                                <!-- <div class="list-item-no" :class="+item.no <=3 ? 'top-no' : ''">{{item.no}}</div> -->
                                <!--
                                    可以采用上面被注释的方式也可以使用下面的方式自定义样式 class
                                    意思是为每个节点赋予类名称 list-item-no
                                    同时只为 item.no <=3 的节点赋予类名称 top-no
                                    通过定制类名 top-no 设置前3名为特殊样式
                                -->
                                <div :class="['list-item-no',+item.no <=3 ? 'top-no':'']">{{item.no}}</div>
                                <div class="list-item-name">{{item.name}}</div>
                                <div class="list-item-money">{{item.money}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </template>
            <template>这里是内容区域</template>
        </el-card>
    </div>
</template>

<script>
    export default {
        data(){
            return{// 设置默认选中的菜单项：销售额，访问量
                activeIndex: '2',
                radioSelect: '今日',
                date:null,
                pickerOptions: {
                    shortcuts: [
                        {text:'最近一周',onClick(picker){
                            const start = new Date()
                            const end = new Date()
                            start.setTime(start.getTime() - 3600 * 24 * 1000 * 7 )
                            picker.$emit('pick',[start,end],true)// 这里的true表示选择后仍然显示日历，默认选择后立即消失不可见
                        }},
                        {text:'最近一个月',onClick(picker){
                            const start = new Date()
                            const end = new Date()
                            start.setTime(start.getTime() - 3600 * 24 * 1000 * 30 )
                            picker.$emit('pick',[start,end])
                        }},
                        {text:'最近三个月',onClick(picker){
                            const start = new Date()
                            const end = new Date()
                            start.setTime(start.getTime() - 3600 * 24 * 1000 * 100 )// 一百天
                            picker.$emit('pick',[start,end])
                        }},
                    ]
                },
                chartOption: {//销售额、访问量 柱状图
                    title: {
                        text: '年度销售额',
                        textStyle: {
                            fontSize: 12,
                            color: '#666'
                        },
                        left: 25,
                        top: 20
                    },
                    xAxis:{
                        type: 'category',
                        data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
                        axisTick: {
                            alignWithLabel: true,
                            lineStyle: {
                                color: '#999'
                            }
                        },
                        axisLine: {
                            lineStyle: {
                                color: '#999'
                            }
                        },
                        axisLabel: {
                            color: '#333'
                        }
                    },
                    yAxis:{
                        axisLine: { show:false },// 隐藏Y轴的线
                        axisTick: { show:false },// 隐藏Y轴的 tick 线
                        // 如果整个Y轴都不要显示，那么
                        // yAxis: { show:false }

                        splitLine: {
                            lineStyle: {
                                type: 'dotted',
                                color: '#eee'
                            }
                        }
                     },
                    series: [
                        {
                            type: 'bar',
                            barWidth: '50%',
                            data: [ 200,250,350,300,250,200,250,200,250,350,300,250 ]
                        }
                    ],
                    color: [ '#3398DB' ],
                    grid: {
                        top: 70,left:60,right:60,bottom:50
                    }
                },
                rankData:[// 第二个card右边的排行榜
                    { no:1,name:'肯德基',money:123456 },
                    { no:2,name:'麦当劳',money:103456 },
                    { no:3,name:'肯德基',money:93456 },
                    { no:4,name:'海底捞',money:53456 },
                    { no:5,name:'西北筱面村',money:51456 },
                    { no:6,name:'汉堡王',money:256 },
                    { no:7,name:'真功夫',money:202 },
                ]
            }
        },
        methods:{
            onMenuSelect(index){
                this.activeIndex = index;
                console.log(`当前选中的菜单项是${this.activeIndex}`);
            }
        }
    }
</script>

<style lang="scss" scoped>
    .sales-view {
        margin-top: 20px;
        .menu-wrapper {
            position: relative;
            display: flex;

            .sales-view-menu {
                width: 100%;
                padding-left: 20px;

                .el-menu-item {
                    height: 50px;
                    line-height: 50px;
                    margin: 0 20px;
                }
            }
            .menu-right {
                position: absolute;
                top:0;right:20px;height: 50px;
                display: flex;
                align-items: center;
                justify-content: flex-end;

                .sales-view-date-picker {
                    margin-left: 20px;
                }
            }
        }

        .sales-view-chart-wrapper {
            display: flex;
            height: 270px;

            .echarts {
                flex: 0 0 70%;
                width: 70%;
                height: 100%;
            }

            .sales-view-list {
                flex: 1;
                width: 100%;
                height: 100%;
                margin-top: 15px;
                overflow: hidden;

                .sales-view-title {
                    margin-top: 20px;
                    font-size: 12px;
                    color: #666;
                    font-weight: 500;
                }

                .list-item-wrapper {
                    margin-top: 15px;
                    .list-item {
                        display: flex;
                        align-items: center;
                        font-size: 12px;
                        height: 20px;
                        padding: 6px 20px 6px 0;

                        .list-item-no {
                            display: flex;
                            align-items: center;
                            justify-content: center;
                            width: 20px;
                            height: 20px;
                            color: #333;

                            &.top-no {
                                background: #000;
                                border-radius: 50%;
                                color: #fff;
                                font-weight: 500;
                            }
                        }

                        .list-item-name {
                            margin-left: 10px;
                            color: #333;
                        }

                        .list-item-money {
                            flex: 1;
                            text-align: right;
                        }
                    }
                }
            }
        }
    }
</style>
